package mysql

import "log"

type RomVersion struct {
	Id      int64  `db:"id"`
	Url     string `db:"url"`
	Version string `db:"version"`
}

type RomVersionMapper struct {
	dataSouce *DataMysqlSourceClinet
}

func NewRomVersionMapper(ds *DataMysqlSourceClinet) (*RomVersionMapper) {
	mapper := &RomVersionMapper{}
	mapper.dataSouce = ds
	return mapper
}

func (rm *RomVersionMapper) SelectByVersion(version string) (*RomVersion) {
	if version == "" {
		log.Println("参数为空")
		return nil
	}
	var romVers [] RomVersion
	e := rm.dataSouce.SqlDB.Select(&romVers, "SELECT "+
		"id,url,version"+
		" FROM device_rom_resources "+
		"WHERE version = ?", version)
	if e != nil {
		log.Println("查询失败！", e)
		return nil
	}

	if len(romVers) == 0 {
		log.Println("版本固件不存在")
		return nil
	}
	romVersion := romVers[0]
	return &romVersion
}

func (rm *RomVersionMapper) DeleteByVersion(version string) (int64) {
	result, e := rm.dataSouce.SqlDB.Exec("DELETE FROM device_rom_resources "+
		"WHERE version = ?", version)
	if e != nil {
		log.Println("删除失败！", e)
	}
	i, e := result.RowsAffected()
	if e != nil {
		log.Println("rows failed", e)
		return 0
	}
	return i
}
